﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace MCPlatform.MotionLib.Factory.LeadShine
{
    public class LTBoard
    {
        private ushort _cardNo;//卡号
        private ushort _cardNum;//卡数量 
                                //  private uint[] _cardTypeList;//卡固件类型数组
                                //  private ushort[] _cardIdList;   //卡硬件ID号数组

        public ushort CardNum { get { return _cardNum; } }
        //   public uint[] CardTypeList { get { return _cardTypeList; } }
        //   public ushort[] CardIdList { get { return _cardIdList; } }  
        public LTBoard(ushort cardNo)
        {
            this._cardNo = cardNo;
        }
        /// <summary>
        /// 初始化板卡
        /// </summary>
        /// <returns>板块个数</returns>
        /// <exception cref="Exception">获取命令异常</exception>
        public static short InitBoard()
        {
            try
            {
                return LTDMC.dmc_board_init();
            }
            catch (Exception ex)
            {

                throw new Exception("命令：" + "LTDMC.dmc_board_init" + ex.Message + "堆栈: " + ex.StackTrace);
            }
        }

        /// <summary>
        /// 冷复位控制卡，其实就是复位后断电
        /// 执行复位操作后，必须关闭控制卡，等待 15 秒方可执行初始化控制卡，否则会出错。
        /// 出错后必须重新执行复位操作，再等待 15 秒后执行初始化控制卡。
        /// </summary>
        /// <returns>错误代码</returns>
        /// <exception cref="Exception">命令异常</exception>
        public short CoolResetBoard()
        {
            try
            {
                return LTDMC.dmc_cool_reset(_cardNo);
            }
            catch (Exception ex)
            {

                throw new Exception("命令：" + "LTDMC.dmc_cool_reset" + ex.Message + "堆栈: " + ex.StackTrace);
            }
        }

        /// <summary>
        /// 热复位运动控制卡，直接软件复位
        /// 执行复位操作后，必须关闭控制卡，等待 15 秒方可执行初始化控制卡，否则会出错。
        /// 出错后必须重新执行复位操作，再等待 15 秒后执行初始化控制卡。
        /// </summary>
        /// <returns>错误代码</returns>
        /// <exception cref="Exception">命令异常</exception>
        public short HotResetBoard()
        {
            try
            {
                return LTDMC.dmc_soft_reset(_cardNo);
            }
            catch (Exception ex)
            {

                throw new Exception("命令：" + "dmc_soft_reset" + ex.Message + "堆栈: " + ex.StackTrace);
            }
        }

        /// <summary>
        /// 关闭运动控制卡
        /// </summary>
        /// <returns>错误代码</returns>
        /// <exception cref="Exception">命令异常</exception>
        public static short CloseBoard()
        {
            try
            {
                return LTDMC.dmc_board_close();
            }
            catch (Exception ex)
            {

                throw new Exception("命令：" + "LTDMC.dmc_board_close" + ex.Message + "堆栈: " + ex.StackTrace);
            }
        }

        /// <summary>
        /// 获取控制卡硬件ID
        /// </summary>
        /// <returns>错误码</returns>
        /// <exception cref="Exception">命令异常</exception>
        public short GetCardInfList(uint[] _cardTypeList, ushort[] _cardIdList)
        {
            try
            {
                return LTDMC.dmc_get_CardInfList(ref _cardNum, _cardTypeList, _cardIdList);
            }
            catch (Exception ex)
            {

                throw new Exception("命令：" + "LTDMC.dmc_get_CardInfList" + ex.Message + "堆栈: " + ex.StackTrace);
            }
        }
        /// <summary>
        /// 获取运动控制卡的总IO数量
        /// </summary>
        /// <param name="TotalIn">I数量</param>
        /// <param name="TotalOut">O数量</param>
        /// <returns>错误码</returns>
        /// <exception cref="Exception">命令异常</exception>

        public short GetTotalIONum(ref ushort TotalIn, ref ushort TotalOut)
        {
            try
            {
                return LTDMC.dmc_get_total_ionum(_cardNo, ref TotalIn, ref TotalOut);
            }
            catch (Exception ex)
            {

                throw new Exception("命令：" + "LTDMC.dmc_get_total_ionum" + ex.Message + "堆栈: " + ex.StackTrace);
            }
        }

    }
}

